﻿using NHibernate.Mapping.ByCode;
using FinancePortal.Domain.Model;

namespace FinancePortal.Domain.Mapping
{
    public class ActivityMapping : EntityMapping<Activity>
    {
        public ActivityMapping()
        {
            Property(x => x.Name, m =>
                {
                    m.Length(100);
                    m.NotNullable(true);
                });

            Property(x => x.Description, m =>
                {
                    m.Length(1000);
                    m.NotNullable(true);
                });

            Property(x => x.ParentActivityId, m =>
            {
                m.NotNullable(false);
                m.Column("ParentId");
            });

            ManyToOne<Activity>(x => x.ParentActivity, m =>
            {
                //m.Cascade(Cascade.All);
                m.NotNullable(false);
                m.Column("ParentId");
                m.Insert(false);
                m.Update(false);
            });

            Bag<Activity>(x => x.ChildActivities,
               cp =>
               {
                   //cp.Cascade(Cascade.None);
                   cp.Lazy(CollectionLazy.NoLazy);
                   cp.Inverse(true);
                   cp.Key(k => k.Column("ParentId"));
               },
                   cr =>
                   {
                       cr.OneToMany(om =>
                       {
                           om.Class(typeof(Activity));
                       }
                       );
                   }
           );

            ManyToOne<Area>(x => x.Area, m =>
                {
                    m.Cascade(Cascade.None);
                    m.Column("AreaId");
                    m.NotNullable(true);
                    m.Insert(false);
                    m.Update(false);
                });

            Property(x => x.AreaId, m =>
                {
                    m.NotNullable(true);
                    m.Column("AreaId");
                });

            //Bag<ActivityResource>(x => x.ActivitiesResources, cp => { }, cr => cr.OneToMany(x => x.Class(typeof(ActivitySchedule))));
        }
    }
}
